package JAVA300.第十一章多线程;

/**
 * 取钱线程不安全
 */

public class thread_14_synchronized1_unsafe1 {
    public static void main(String[] args) {

        Account account = new Account(500,"hqqs money");
        Drawing hqq = new Drawing(account,100,"hqq-hpv");
        Drawing qyh = new Drawing(account,300,"qyh-nike");
        hqq.start();
        qyh.start();


    }

}

class Account {
     int money;
     String name;

    public Account(int money, String name) {
        this.money = money;
        this.name = name;
    }
}

class Drawing extends Thread{
    Account account;//取得账户
    int drawingMoney;//单次取得钱数
    int gettotal;//总共取得钱数

    public Drawing(Account account, int drawingMoney,String name) {
        super(name);
        this.account = account;
        this.drawingMoney = drawingMoney;
    }

    @Override
    public void run() {

        //添加安全控制--防止出现负数-控制不住 引出第三个不安全类
        if ( account.money - drawingMoney <0){
            return;
        }

        //模拟网络延时
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        gettotal += drawingMoney;
        System.out.println(this.getName()+"-->账户余额为："+account.money);
        System.out.println(this.getName()+"-->口袋的钱为："+gettotal);

    }
}


